app.controller("chapterCategoryController", function ($scope, $controller, $location, chapterCategoryService) {
    $controller('baseController', {$scope: $scope, baseService: chapterCategoryService});

    $scope.editIsuse = function (id, val, msg) {
        var bool = true;
        if (msg) {
            bool = window.confirm(msg);
        }
        if (bool) {
            var queryJson = {
                entity: {
                    id: id,
                    isuse: val
                }
            };
            chapterCategoryService.edit(queryJson).success(function (res) {
                $scope.resHandler(res, function (res) {
                    alert("修改成功");
                    $scope.findPage();
                })
            })
        }

    }

    $scope.batchEditIsuse = function (val, msg) {
        var bool = true;
        if (msg) {
            bool = window.confirm(msg);
        }
        if (bool) {
            var queryJson = {
                entity: {
                    isuse: val
                },
                ids : $scope.ids
            };
            chapterCategoryService.updateManyByIds(queryJson).success(function (res) {
                $scope.resHandler(res, function (res) {
                    alert("修改成功");
                    $('.js_checkbox').prop("checked", false);
                    $scope.findPage();
                })
            })
        }

    }


    $scope.entity = {};

    $scope.$watch("level", function (newValue, oldValue) {
        if (parseInt(newValue) === 2) {
            if(!$scope.entity.id){
                $scope.entity.pid = "0";
            } else{
                $scope.entity.pid = $scope.entity.pid.toString();
            }
        } else if (parseInt(newValue) === 1) {
            $scope.entity.pid = "0";
        } else {
            delete $scope.entity.pid;
        }
    });

    $scope.edit = function (direct) {
        var entity = $scope.entity;

        if (entity.pid === undefined) {
            alert("请选择分类等级");
            return;
        }
        if (entity.name === undefined || entity.name === null || entity.name === "") {
            alert("请输入分类名称");
            return;
        }

        var queryJson = {
            entity: entity
        };
        chapterCategoryService.edit(queryJson).success(function (res) {
            $scope.resHandler(res, function (res) {
                if (direct === undefined || direct === null) {
                    $scope.entity = {};
                    alert(res.msg);
                } else {
                    location.href = direct;
                }
            })
        })
    }

    $scope.editInit = function () {
        chapterCategoryService.getCategoryList({others:{level:1}}).success(function (res) {
            $scope.resHandler(res, function (res) {
                $scope.firstCategoryList = res.data;
            });
        });

        $params = $location.search();
        if($params['id'] !== undefined) {
            var queryJson = {
                entity : {
                    id : $params['id']
                }
            };
            chapterCategoryService.find(queryJson).success(function (res) {
                $scope.resHandler(res, function () {
                   $scope.entity = res.data;
                   if($scope.entity.pid === 0){
                       $("#level").val("1").select2();
                       $scope.level = "1";
                   } else {
                       $("#level").val("2").select2();
                       $scope.level = "2";
                       $("#pid").val($scope.entity.pid).select2();
                   }
                });
            });
        }
    }
});
